package com.joolun.web.controller.activity;

import java.util.List;

import com.joolun.common.core.domain.entity.SysUser;
import com.joolun.common.utils.DateUtils;
import com.joolun.common.utils.SecurityUtils;
import me.chanjar.weixin.common.util.DataUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.joolun.common.annotation.Log;
import com.joolun.common.core.controller.BaseController;
import com.joolun.common.core.domain.AjaxResult;
import com.joolun.common.enums.BusinessType;
import com.joolun.activity.domain.ActivityAwardInfo;
import com.joolun.activity.service.IActivityAwardInfoService;
import com.joolun.common.utils.poi.ExcelUtil;
import com.joolun.common.core.page.TableDataInfo;

/**
 * 活动奖品明细Controller
 *
 * @author www.joolun.com
 * @date 2022-11-22
 */
@RestController
@RequestMapping("/activity/awardInfo")
public class ActivityAwardInfoController extends BaseController
{
    @Autowired
    private IActivityAwardInfoService activityAwardInfoService;

    /**
     * 查询活动奖品明细列表
     */
    @PreAuthorize("@ss.hasPermi('activity:awardInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(ActivityAwardInfo activityAwardInfo)
    {
        startPage();
        List<ActivityAwardInfo> list = activityAwardInfoService.selectActivityAwardInfoList(activityAwardInfo);
        return getDataTable(list);
    }

    /**
     * 导出活动奖品明细列表
     */
    @PreAuthorize("@ss.hasPermi('activity:awardInfo:export')")
    @Log(title = "活动奖品明细", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ActivityAwardInfo activityAwardInfo)
    {
        List<ActivityAwardInfo> list = activityAwardInfoService.selectActivityAwardInfoList(activityAwardInfo);
        ExcelUtil<ActivityAwardInfo> util = new ExcelUtil<ActivityAwardInfo>(ActivityAwardInfo.class);
        return util.exportExcel(list, "awardInfo");
    }

    /**
     * 获取活动奖品明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('activity:awardInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(activityAwardInfoService.selectActivityAwardInfoById(id));
    }

    /**
     * 核销
     */
    @GetMapping(value = "/check/{id}")
    public AjaxResult checkInfo(@PathVariable("id") String id)
    {
        ActivityAwardInfo activityAwardInfo= activityAwardInfoService.getById(id);
        if(null==activityAwardInfo){
            return AjaxResult.error("信息不存在");
        }
        if(activityAwardInfo.getStatus().equals("1")){
            return AjaxResult.error("未中奖不能核销");
        }
        if(activityAwardInfo.getStatus().equals("3")){
            return AjaxResult.error("请勿重复核销");
        }
        SysUser sysUser= SecurityUtils.getLoginUser().getUser();
        activityAwardInfo.setUserId(sysUser.getUserId());
        activityAwardInfo.setCheckUser(sysUser.getUserName());
        activityAwardInfo.setDeptId(sysUser.getDept().getDeptId());
        activityAwardInfo.setDeptName(sysUser.getDept().getDeptName());
        activityAwardInfo.setCheckTime(DateUtils.getNowDate());
        activityAwardInfo.setStatus("3");
        return AjaxResult.success(activityAwardInfoService.updateById(activityAwardInfo));
    }

    /**
     * 新增活动奖品明细
     */
    @PreAuthorize("@ss.hasPermi('activity:awardInfo:add')")
    @Log(title = "活动奖品明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ActivityAwardInfo activityAwardInfo)
    {
        return toAjax(activityAwardInfoService.insertActivityAwardInfo(activityAwardInfo));
    }

    /**
     * 修改活动奖品明细
     */
    @PreAuthorize("@ss.hasPermi('activity:awardInfo:edit')")
    @Log(title = "活动奖品明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ActivityAwardInfo activityAwardInfo)
    {
        return toAjax(activityAwardInfoService.updateActivityAwardInfo(activityAwardInfo));
    }

    /**
     * 删除活动奖品明细
     */
    @PreAuthorize("@ss.hasPermi('activity:awardInfo:remove')")
    @Log(title = "活动奖品明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(activityAwardInfoService.deleteActivityAwardInfoByIds(ids));
    }
}
